<template>
  <!-- 更新早班会记录 -->
  <base-breadcrumb>
    <div class="page-body">
      <a-card :bordered="false">
        <title-name title="基本信息" />
        <a-form-model ref="form" :model="formInfo" :rules="rules" :hideRequiredMark="true">
          <a-row :gutter="24">
            <a-col :md="8" :sm="8">
              <a-form-model-item
                label="所属分公司"
                :labelCol="{lg: {span: 6}, sm: {span:6}}"
                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="branchComId"
              >
                <span v-if="pageType === 'look'">{{formInfo.branchComName}}</span>
                <SelectSubCompany v-else v-model="formInfo.branchComId" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item
                label="所属项目"
                :labelCol="{lg: {span: 6}, sm: {span:6}}"
                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="projectId"
              >
                <span v-if="pageType === 'look'">{{formInfo.projectName}}</span>
                <SelectProject
                  v-else
                  v-model="formInfo.projectId"
                  keyValue="branchComId"
                  :form="formInfo"
                  :clear-list="true"
                />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item
                label="日期"
                :labelCol="{lg: {span: 6}, sm: {span:6}}"
                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="morningTime"
              >
                <a-date-picker v-model="formInfo.morningTime" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :md="8" :sm="8">
              <a-form-model-item
                label="作业部位"
                :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="workPart"
              >
                <BaseInput placeholder="请输入" v-model="formInfo.workPart" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item
                label="分包商"
                :labelCol="{lg: {span: 6}, sm: {span:6}}"
                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput disabled v-model="formInfo.businessName" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item
                label="班组"
                :labelCol="{lg: {span: 6}, sm: {span:6}}"
                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput disabled v-model="formInfo.groupName" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :md="8" :sm="8">
              <a-form-model-item
                label="班组长"
                :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
              >
                <BaseInput disabled v-model="formInfo.teamLeader" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item
                label="班组总人数"
                :labelCol="{lg: {span: 6}, sm: {span:6}}"
                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="teamSize"
              >
                <BaseInput placeholder="请输入" v-model="formInfo.teamSize" />
              </a-form-model-item>
            </a-col>
            <a-col :md="8" :sm="8">
              <a-form-model-item
                label="实际参会人数"
                :labelCol="{lg: {span: 6}, sm: {span:6}}"
                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="actInvolveSize"
              >
                <BaseInput placeholder="请输入" v-model="formInfo.actInvolveSize" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :span="24">
              <a-form-model-item
                label="未参会人员名单"
                :labelCol="{ lg: { span: 2 }, sm: { span: 2} }"
                :wrapperCol="{ lg: { span: 22 }, sm: { span: 22 } }"
                prop="notInvolved"
              >
                <a-col :span="24">
                  <BaseTextarea
                    style="resize:none;"
                    placeholder="请输入"
                    :rows="2"
                    v-model="formInfo.notInvolved"
                  />
                </a-col>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :span="24">
              <a-form-model-item
                label="拍照上传"
                :labelCol="{ lg: { span: 2 }, sm: { span: 2 } }"
                :wrapperCol="{ lg: { span: 22 }, sm: { span: 22 } }"
              >
                <upload-list
                  :businessCode="'SHE07'"
                  :editModel="filterEditModel(pageType)"
                  :uploadArrys.sync="formInfo.fileInfoList"
                  uploadType="image"
                  multiple
                ></upload-list>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-card>
      <div class="tool-btns page-btn-right-top" v-if="pageType !== 'look'">
        <a-button type="danger" v-if="pageType !== 'add'" :loading="loading.DELETE" @click="handleSave('DELETE')">删除草稿</a-button>
        <a-button type="primary" :loading="loading.DRAFT" @click="handleSave('DRAFT')">保存草稿</a-button>
        <a-button type="primary" :loading="loading.SUBMITTED" @click="handleSave('SUBMITTED')">提交</a-button>
        <a-button type="primary" @click="handleClose">关闭</a-button>
      </div>
      <div class="tool-btns page-btn-right-top" v-else>
        <a-button type="primary" @click="handleClose">关闭</a-button>
      </div>
    </div>
  </base-breadcrumb>
</template>

<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany.vue'
import SelectProject from '@/components/Select/SelectProject.vue'
import UploadList from '@/components/UploadList'
import {
  getMeetingRecordDetail,
  addMeetingRecord,
  deleteMeetingRecord,
  updateMeetingRecord,
} from '@/api/safetySupervision/morningMeeting.js'
import Vue from 'vue'

export default {
    name:'a'+Date.now(),  components: {
    UploadList,
    SelectSubCompany,
    SelectProject,
  },
  data() {
    return {
      loading: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
      },
      formInfo: {
        branchComId: '',
        projectId: '',
        morningTime: '',
        workPart: '',
        teamSize: '',
        actInvolveSize: '',
        notInvolved: '',
        fileInfoList: [],
      },
      rules: {
        branchComId: [{ required: true, message: '请选择', trigger: 'change' }],
        projectId: [{ required: true, message: '请选择', trigger: 'change' }],
        morningTime: [{ required: true, message: '请选择', trigger: 'change' }],
        workPart: [{ required: true, message: '请输入', trigger: 'change' }],
        teamSize: [{ required: true, message: '请输入', trigger: 'change' }],
        actInvolveSize: [{ required: true, message: '请输入', trigger: 'change' }]
      },
    }
  },
  computed: {
    pageType() {
      return this.$route.query.pageType || 'look'
    },
  },
  created() {
    this.getDetail()
    this.initDisabledInfo()
  },
  methods: {
    // 初始化禁止填写的信息
    initDisabledInfo() {
      const userInfo = Vue.getAcloudProvider().getUserInfo()
      const { name } = userInfo
      this.formInfo.teamLeader = name
    },
    filterEditModel(type) {
      let flag = ''
      switch (type) {
        case 'add':
          flag = 2
          break
        case 'edit':
          flag = 1
          break
        case 'look':
          flag = 0
          break
        default:
          flag = 0
          break
      }
      return flag
    },
    validateForm() {
      let flag = false
      this.$refs.form.validate((valid) => {
        flag = valid
      })
      return flag
    },
    async getDetail() {
      const { id } = this.$route.query
      if (!id) return
      const params = {
        id,
      }
      const [err, res] = await getMeetingRecordDetail(params)
      if (!err) {
        this.formInfo = {
          ...this.formInfo,
          ...res.data,
        }
        this.$nextTick(() => {
          this.formInfo.projectId = res.data.projectId.toString()
        })
        this.initDisabledInfo()
      }
    },
    // 处理增、删、改操作
    async handleSave(status) {
      if (status !== 'DELETE' && !this.validateForm()) return
      if (this.loading[status]) return
      this.loading[status] = true
      const params = {
        ...this.formInfo,
        status,
        id: this.$route.query.id || void 0,
      }
      const { pageType } = this.$route.query
      const fn = pageType === 'edit' ? updateMeetingRecord : addMeetingRecord
      const map = {
        DELETE: deleteMeetingRecord,
        DRAFT: fn,
        SUBMITTED: fn,
      }
      const [err, res] = await map[status](params)
      if (!err) {
        this.$message.success('成功')
        this.handleClose()
      }
      this.loading[status] = false
    },
    handleClose() {
      this.$multiTab.close(this.$route.fullpath)
    },
  },
}
</script>

<style lang="less" scoped>
@deep: ~'>>>';
.page-body {
  .ant-radio-group {
    width: 100%;
  }
  form .ant-mentions,
  form textarea.ant-input {
    height: auto !important;
    width: 100%;
  }
}
.row-title {
  font-weight: bold;
}
.tool-btns {
  display: flex;
  justify-content: flex-end;
  @{deep} .ant-btn + .ant-btn {
    margin-left: 12px;
  }
}
</style>